import java.util.*;

/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/8/7 9:40
 */
public class Solution102 {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    public List<List<Integer>> levelOrder(TreeNode root) {
        orderSearch(root);
        return res;
    }

    private void orderSearch(TreeNode root) {
        if (root == null) return;
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> paths = new ArrayList<Integer>();
            int len = queue.size();
            while (len > 0) {
                TreeNode poll = queue.poll();
                paths.add(poll.val);
                if (poll.left != null) queue.offer(poll.left);
                if (poll.right != null) queue.offer(poll.right);
                len--;
            }
            res.add(paths);
        }
    }
}
